package com.security.config.security;

import com.security.config.filter.CoStomExpiredSessionStrategy;
import com.security.config.mobile.MobileAuthenticationConfig;
import com.security.system.service.SysUserService;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.session.InvalidSessionStrategy;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.annotation.Resource;

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    SysUserService service;

    @Resource
    PasswordEncoder passwordEncoder;

    @Resource
    AuthenticationSuccessHandler securitySuccessHandler;

    @Resource
    AuthenticationFailureHandler securityFailureHandler;

    @Resource
    InvalidSessionStrategy costomInvalidSessionStrategy;

    @Resource
    OncePerRequestFilter imgCodeFilter;

    @Resource
    OncePerRequestFilter mobileCodeFilter;

    @Resource
    MobileAuthenticationConfig mobileAuthenticationConfig;


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(service).passwordEncoder(passwordEncoder);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers("/layui/**","/system/**")
                .antMatchers("/login/captcha","/mobile/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.addFilterBefore(mobileCodeFilter, UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(imgCodeFilter, UsernamePasswordAuthenticationFilter.class)
                .authorizeRequests()
                /*设置 只允许 拥有 访问的 URL 的才能访问 */
                .anyRequest().access("@arbcServiceFilter.hasPermission(request,authentication)")
                .and()
                /*设置 表单登录*/
                .formLogin()
                /*设置 自定义 登录 页面路劲 get 请求*/
                .loginPage("/login")
                /*设置 认证请求路径 默认 "/login" */
                .loginProcessingUrl("/login")
                .successHandler(securitySuccessHandler)
                .failureHandler(securityFailureHandler)
                .permitAll()
                .and()
                .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login")
                .invalidateHttpSession(true)
                .permitAll()
                .and()
                .rememberMe()
                //.tokenRepository(jdbcTokenRepository())
                .tokenValiditySeconds(60*60*24*7) //配置记住我 的时间 单位 秒
                .and()
                .sessionManagement()
                /*设置 session 制创建 机*/
                .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
                /*会话超时 返回 URL*/
                .invalidSessionStrategy(costomInvalidSessionStrategy)
                .sessionFixation()
                .migrateSession()
                /*设置 同账号多端登录被下线需求*/
                .maximumSessions(1)
                .maxSessionsPreventsLogin(false)
                .expiredSessionStrategy(new CoStomExpiredSessionStrategy())
                .and()
                .and()
                .csrf().disable()
                /*解决 iframe 不加载 的问题 */
                .headers().frameOptions().disable();
        http.apply(mobileAuthenticationConfig);
    }
}
